<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/13 0013
 * Time: 10:00
 * 预约练车配置
 * @param 1、周一到周六，2、周天
 */
namespace app\admin\Controller;
use think\db;
use think\Controller;
class Yuyuetype extends Controller{
    //预约配置列表
    public function yuyueTypeList(){
        return $this->fetch();
    }
    public function yuyueListData($page,$limit){
        $get=input('get.');
        $where['del']=0;
        $list=Db::name('yuyue_type')
            ->where($where)
            ->page($page,$limit)
            ->select();
        $count=Db::name('yuyue_type')->count();
        return json(['code'=>0,'msg'=>'返回成功','count'=>$count,'data'=>$list]);
    }
    //添加预约配置
    public function yuyueTypeAdd(){
        return $this->fetch();
    }
    public function yueyueTypeAddForm(){
        $post=input('post.');
        if($post){
            //周天配置
            if(!empty($post['work_time1'])){
                $data['work_time']=$post['work_time1'];
                $data['rest_time']=$post['rest_time1'];
                $data['type']=1;
            }else{
                //周一到周六配置
                $data['work_time']=$post['work_time'];
                $data['rest_time']=$post['rest_time'];
                $data['count']=$post['count'];
                $data['free_count']=$post['free_count'];
                $data['pay_count']=$post['pay_count'];
                $data['shichang']=$post['shichang'];
                $data['coachcount']=$post['coachcount'];
                $data['type']=0;
            }
            $flag=Db::name('yuyue_type')->insert($data);
            if($flag){
                return json(['code'=>1,'msg'=>'添加成功']);
            }else{
                return json(['code'=>0,'msg'=>'添加失败']);
            }
        }
    }
    //修改预约配置
    public function yuyueTypeEdit(){
        $id=input('get.id');
        $yuyue=Db::name('yuyue_type')
            ->where('id',$id)
            ->find();
        $this->assign('data',$yuyue);
        return $this->fetch();
    }
    public function yueyueTypeEditForm(){
        $post=input('post.');
        $temp=Db::name('yuyue_type')
            ->where('id',$post['id'])
            ->find();
        if($post){
            if($temp['type']==0){
                $data['count']=$post['count'];
                $data['free_count']=$post['free_count'];
                $data['pay_count']=$post['pay_count'];
                $data['shichang']=$post['shichang'];
                $data['coachcount']=$post['coachcount'];
                $data['work_time']=$post['work_time'];
                $data['rest_time']=$post['rest_time'];
            }else{
                $data['work_time']=$post['work_time1'];
                $data['rest_time']=$post['rest_time1'];
            }
            $flag=Db::name('yuyue_type')
                ->where('id',$post['id'])
                ->update($data);
            if($flag){
                return json(['code'=>1,'msg'=>'修改成功']);
            }else{
                return json(['code'=>0,'msg'=>'修改失败']);
            }
        }
    }
    //删除预约配置
    public function delYuyueType(){
        $post=input('post.');
        $flag=Db::name('yuyue_type')
            ->where('id','in',$post['ids'])
            ->update(array('del'=>1));
        if($flag){
            return json(['code'=>1,'msg'=>'删除成功']);
        }
        return json(['code'=>0,'msg'=>'删除失败']);
    }
}
